package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.piaohao.model.User;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 *
 * @author piaohao
 */
public class UserController extends Controller {

    public static final String moduleName = "用户模块";

    public void list() {
        try {
            String orgId = (String) getSessionAttr("orgId");
            Integer start = getParaToInt("start");
            Integer limit = getParaToInt("limit");
            List<Record> records = null;
            Long total = 0L;
            if (orgId.equals("0")) {
                records = Db.find("SELECT r1.*,o.name orgName from \n"
                        + "(select u.*,e.name emp_name,e.org_id\n"
                        + "	from t_user u \n"
                        + "	left join t_employee e \n"
                        + "	on u.emp_id=e.id) r1\n"
                        + "LEFT JOIN t_organization o\n"
                        + "ON r1.org_id=o.id limit ?,?", start, limit);
                total = Db.queryLong("select count(*) from t_user u left join t_employee e on u.emp_id=e.id");
            } else {
                records = Db.find("SELECT r1.*,o.name orgName from \n"
                        + "(select u.*,e.name emp_name,e.org_id\n"
                        + "	from t_user u \n"
                        + "	left join t_employee e \n"
                        + "	on u.emp_id=e.id) r1\n"
                        + "LEFT JOIN t_organization o\n"
                        + "ON r1.org_id=o.id where o.id=? limit ?,?", orgId, start, limit);
                total = Db.queryLong("select count(*) from t_user u left join t_employee e on u.emp_id=e.id where e.org_id=?", orgId);
            }
            renderJson(JsonUtil.printGrid(total, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void add() {
        try {
            User user = getModel(User.class);
            user.set("id", UUIDGenerator.getUUID());
            user.save();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            User user = getModel(User.class);
            user.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String[] idArr = getParaValues("ids");
            for (int i = 0; i < idArr.length; i++) {
                User.dao.deleteById(idArr[i]);
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void getUserByEmpId() {
        try {
            User user = User.dao.findFirst("select * from t_user where emp_id=?", getPara("empId"));
            Map map = new HashMap();
            map.put("user", user);
            renderJson(JsonUtil.printSuccess(true, "成功", map));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }
}
